<?php

//No incluye el common.php porque sino dbcreate.php al incluir este falla.

// Delete the current cookie
setcookie("iduser", "", time()-3600);
setcookie("username", "", time()-3600);
setcookie("lang", "", time()-3600);

?>
<p>Instrucciones: en phpMyAdmin crear el siguiente usuario:<br />
	Nombre de usuario: "timdbuser"<br />
	Contrasena: "hola"<br />
	Host: "localhost"<br />
	Permisos: asignarle todos los permisos.</p>
<p>Dar permisos de escritura al fichero del chat.</p>

<?php
// Create the DB connection
require 'include/dbconnect.php';

// Create the DB if it doesn't exist
$result = mysql_query("CREATE DATABASE IF NOT EXISTS ".$db_name,$con);
if (!$result)
{
	die('Could not create DB: ' . mysql_error());
}

// Select the DB for next queries
$result = mysql_select_db($db_name, $con);
if (!$result)
{
	die('Could not select DB: ' . mysql_error());
}

// Create the users table
$result = mysql_query("DROP TABLE IF EXISTS users");
if (!$result)
{
	die('Could not drop DB table "users": ' . mysql_error());
}

$query = "CREATE TABLE users
(
iduser int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(iduser),
username varchar(15),
password varchar(15),
mail varchar(30),
lang varchar(2)
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "users": ' . mysql_error());
}

//// Inser a first user
//$result = mysql_query("INSERT INTO users (username, password)
//VALUES ('tim', 'tim')");
//if (!$result)
//{
//	die('Invalid query: ' . mysql_error());
//}

// Create the cities table
$result = mysql_query("DROP TABLE IF EXISTS cities");
if (!$result)
{
	die('Could not drop DB table "cities": ' . mysql_error());
}

$query = "CREATE TABLE cities
(
idcity int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(idcity),
namecity VARCHAR(40),
iduser int,
coordx int,
coordy int,
box0 int,
box0lvl int,
box1 int,
box1lvl int,
box2 int,
box2lvl int,
box3 int,
box3lvl int,
box4 int,
box4lvl int,
box5 int,
box5lvl int,
box6 int,
box6lvl int,
box7 int,
box7lvl int,
box8 int,
box8lvl int,
box9 int,
box9lvl int,
box10 int,
box10lvl int,
box11 int,
box11lvl int,
box12 int,
box12lvl int,
box13 int,
box13lvl int,
box14 int,
box14lvl int,
box15 int,
box15lvl int,
tropas0  int,
tropas1 int,
tropas2 int,
tropas3 int,
tropas4 int,
grano int,
hierro int,
oro int,
madera int,
last_resources_update int DEFAULT 0
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "cities": ' . mysql_error());
}

  //Create attacks table
$result = mysql_query("DROP TABLE IF EXISTS attacks");
if (!$result)
{
    die('Could not drop DB table "attacks": ' . mysql_error());
}
$query = "CREATE TABLE attacks
(
idataque int NOT NULL AUTO_INCREMENT,
id_ciudad_atacante int,
id_ciudad_defensor int,
name_ciudad_defensor VARCHAR(40),
hora_llegada int,
tropas0 int,
tropas1 int,
tropas2 int,
tropas3 int,
tropas4 int,
PRIMARY KEY(idataque)
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "attacks": ' . mysql_error());
}

 //Create tropas volviendo
$result = mysql_query("DROP TABLE IF EXISTS returning");
if (!$result)
{
    die('Could not drop DB table "returning": ' . mysql_error());
}
$query = "CREATE TABLE returning
(
idreturning int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(idreturning),
id_ciudad int,
hora_llegada int,
tropas0 int,
tropas1 int,
tropas2 int,
tropas3 int,
tropas4 int
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "returning": ' . mysql_error());
}

//Create edificios en construccion
$result = mysql_query("DROP TABLE IF EXISTS building");
if (!$result)
{
    die('Could not drop DB table "building": ' . mysql_error());
}
$query = "CREATE TABLE building
(
idbuilding int NOT NULL AUTO_INCREMENT,
id_city int,
id_square int,
type_building int,
next_level int,
end_time int,
PRIMARY KEY(idbuilding)
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "building": ' . mysql_error());
}

//Create tropas en construccion
$result = mysql_query("DROP TABLE IF EXISTS creating");
if (!$result)
{
    die('Could not drop DB table "creating": ' . mysql_error());
}
$query = "CREATE TABLE creating
(
idcreating int NOT NULL AUTO_INCREMENT,
id_city int,
end_time int,
troop_type int,
PRIMARY KEY(idcreating)
)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "creating": ' . mysql_error());
}


// Create the mensajes table
$result = mysql_query("DROP TABLE IF EXISTS mensajes");
if (!$result)
{
	die('Could not drop DB table "mensajes": ' . mysql_error());
}

$query = "CREATE TABLE mensajes
(
ID_Mensaje int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(ID_Mensaje),
ID_Emisor int,
ID_Receptor int,
Asunto VARCHAR(120),
Texto VARCHAR(600),
Fecha VARCHAR(60)


)";

$result = mysql_query($query,$con);
if (!$result)
{
	die('Could not create DB table "mensajes": ' . mysql_error());
}



// Chat

$result = mysql_query("DROP TABLE IF EXISTS chat_rooms");
if (!$result)
{
    die('Could not drop DB table: ' . mysql_error());
}

$result = mysql_query("DROP TABLE IF EXISTS chat_users");
if (!$result)
{
    die('Could not drop DB table: ' . mysql_error());
}

$result = mysql_query("DROP TABLE IF EXISTS chat_users_rooms");
if (!$result)
{
    die('Could not drop DB table: ' . mysql_error());
}

$result = mysql_query('CREATE TABLE IF NOT EXISTS chat_rooms (
  id tinyint(4) NOT NULL AUTO_INCREMENT,
  name varchar(20) NOT NULL,
  numofuser int(10) NOT NULL,
  file varchar(30) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3');
if (!$result)
{
	die('Could not create the chats tables: ' . mysql_error());
}

$result = mysql_query('CREATE TABLE IF NOT EXISTS chat_users (
  id tinyint(10) NOT NULL AUTO_INCREMENT,
  username varchar(100) NOT NULL,
  status tinyint(1) NOT NULL,
  time_mod int(100) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY username (username)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23');
if (!$result)
{
	die('Could not create the chats tables: ' . mysql_error());
}

$result = mysql_query('CREATE TABLE IF NOT EXISTS chat_users_rooms (
  id int(100) NOT NULL AUTO_INCREMENT,
  username varchar(100) NOT NULL,
  room varchar(100) NOT NULL,
  mod_time int(40) NOT NULL,
  PRIMARY KEY (id)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1472 ;');
if (!$result)
{
	die('Could not create the chats tables: ' . mysql_error());
}

$result = mysql_query("INSERT INTO chat_rooms VALUES(1, 'General',100,'general.txt')");
if (!$result)
{
	die('Could not create the chats tables: ' . mysql_error());
}

// Config
$result = mysql_query("DROP TABLE IF EXISTS config");
if (!$result)
{
    die('Could not drop DB table: ' . mysql_error());
}
$result = mysql_query('CREATE TABLE config (
  start_time int
);');
if (!$result)
{
	die('Could not create the config tables: ' . mysql_error());
}

$result = mysql_query("INSERT INTO config(start_time) VALUES(".time().")");
if (!$result)
{
	die('Could not insert into config: ' . mysql_error());
}

// Close connection
require 'include/dbdisconnect.php';

echo '<p>DB created successfully.</p>';
?>
